From 2346ccde436b752061d0376b84dcbb663fc50ce8 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 5 Jun 2014 08:31:06 -0400 Subject: [PATCH] inspector: show GMenus This does not quite work yet, and I have no idea why. --- gtk/inspector/Makefile.am | 3 + gtk/inspector/init.c | 2 + gtk/inspector/inspector.gresource.xml | 1 + gtk/inspector/menu.c | 118 ++++++++++++++++++++++++++ gtk/inspector/menu.ui | 83 ++++++++++++++++++ gtk/inspector/window.c | 5 +- gtk/inspector/window.h | 1 + gtk/inspector/window.ui | 14 +++ po/POTFILES.in | 1 + 9 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 gtk/inspector/menu.c create mode 100644 gtk/inspector/menu.ui diff --git a/gtk/inspector/Makefile.am b/gtk/inspector/Makefile.am index 896da9c4f7..72777d79b9 100644 --- a/gtk/inspector/Makefile.am +++ b/gtk/inspector/Makefile.am @@ -35,6 +35,8 @@ libgtkinspector_la_SOURCES = \ init.h \ init.c \ inspect-button.c \ + menu.h \ + menu.c \ object-hierarchy.h \ object-hierarchy.c \ prop-editor.h \ @@ -91,6 +93,7 @@ templates = \ css-editor.ui \ data-list.ui \ general.ui \ + menu.ui \ object-hierarchy.ui \ prop-list.ui \ resource-list.ui \ diff --git a/gtk/inspector/init.c b/gtk/inspector/init.c index 08952aab3d..4fdbbcd7b0 100644 --- a/gtk/inspector/init.c +++ b/gtk/inspector/init.c @@ -29,6 +29,7 @@ #include "data-list.h" #include "general.h" #include "gestures.h" +#include "menu.h" #include "object-hierarchy.h" #include "prop-list.h" #include "python-hooks.h" @@ -57,6 +58,7 @@ gtk_inspector_init (void) g_type_ensure (GTK_TYPE_INSPECTOR_DATA_LIST); g_type_ensure (GTK_TYPE_INSPECTOR_GENERAL); g_type_ensure (GTK_TYPE_INSPECTOR_GESTURES); + g_type_ensure (GTK_TYPE_INSPECTOR_MENU); g_type_ensure (GTK_TYPE_INSPECTOR_OBJECT_HIERARCHY); g_type_ensure (GTK_TYPE_INSPECTOR_PROP_LIST); g_type_ensure (GTK_TYPE_INSPECTOR_PYTHON_SHELL); diff --git a/gtk/inspector/inspector.gresource.xml b/gtk/inspector/inspector.gresource.xml index 135d2b0384..774161ba7d 100644 --- a/gtk/inspector/inspector.gresource.xml +++ b/gtk/inspector/inspector.gresource.xml @@ -7,6 +7,7 @@ css-editor.ui data-list.ui general.ui + menu.ui object-hierarchy.ui prop-list.ui resource-list.ui diff --git a/gtk/inspector/menu.c b/gtk/inspector/menu.c new file mode 100644 index 0000000000..d73d3f26fd --- /dev/null +++ b/gtk/inspector/menu.c @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2014 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "config.h" +#include +#include "menu.h" +#include "gtkwidgetprivate.h" + +enum +{ + COLUMN_TYPE, + COLUMN_LABEL, + COLUMN_ACTION, + COLUMN_TARGET, + COLUMN_ICON +}; + +struct _GtkInspectorMenuPrivate +{ + GtkTreeStore *model; +}; + +G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorMenu, gtk_inspector_menu, GTK_TYPE_BOX) + +static void +gtk_inspector_menu_init (GtkInspectorMenu *sl) +{ + sl->priv = gtk_inspector_menu_get_instance_private (sl); + gtk_widget_init_template (GTK_WIDGET (sl)); +} + +static void +add_item (GtkInspectorMenu *sl, + GMenuModel *menu, + gint idx, + GtkTreeIter *parent) +{ + GtkTreeIter iter; + GVariant *value; + gchar *label = NULL; + gchar *action = NULL; + gchar *target = NULL; + gchar *icon = NULL; + + g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_LABEL, "s", &label); + g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_ACTION, "s", &action); + value = g_menu_model_get_item_attribute_value (menu, idx, G_MENU_ATTRIBUTE_TARGET, NULL); + if (value) + { + target = g_variant_print (value, FALSE); + g_variant_unref (value); + } + + gtk_tree_store_append (sl->priv->model, &iter, parent); + gtk_tree_store_set (sl->priv->model, &iter, + COLUMN_TYPE, "item", + COLUMN_LABEL, label, + COLUMN_ACTION, action, + COLUMN_TARGET, target, + COLUMN_ICON, icon, + -1); + + g_free (label); + g_free (action); + g_free (target); + g_free (icon); +} + +static void +add_menu (GtkInspectorMenu *sl, + GMenuModel *menu, + GtkTreeIter *parent) +{ + gint n_items; + gint i; + + gtk_widget_show (GTK_WIDGET (sl)); + + n_items = g_menu_model_get_n_items (menu); + for (i = 0; i < n_items; i++) + add_item (sl, menu, i, parent); +} + +void +gtk_inspector_menu_set_object (GtkInspectorMenu *sl, + GObject *object) +{ + gtk_widget_hide (GTK_WIDGET (sl)); + gtk_tree_store_clear (sl->priv->model); + + if (G_IS_MENU_MODEL (object)) + add_menu (sl, G_MENU_MODEL (object), NULL); +} + +static void +gtk_inspector_menu_class_init (GtkInspectorMenuClass *klass) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/menu.ui"); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorMenu, model); +} + +// vim: set et sw=2 ts=2: diff --git a/gtk/inspector/menu.ui b/gtk/inspector/menu.ui new file mode 100644 index 0000000000..b63ee92f09 --- /dev/null +++ b/gtk/inspector/menu.ui @@ -0,0 +1,83 @@ + + + + + + + + + + + + + diff --git a/gtk/inspector/window.c b/gtk/inspector/window.c index ff0f9b2240..eee2712d0a 100644 --- a/gtk/inspector/window.c +++ b/gtk/inspector/window.c @@ -36,9 +36,10 @@ #include "button-path.h" #include "size-groups.h" #include "data-list.h" -#include "gestures.h" #include "signals-list.h" #include "actions.h" +#include "menu.h" +#include "gestures.h" G_DEFINE_TYPE (GtkInspectorWindow, gtk_inspector_window, GTK_TYPE_WINDOW) @@ -78,6 +79,7 @@ on_widget_tree_selection_changed (GtkInspectorWidgetTree *wt, gtk_inspector_size_groups_set_object (GTK_INSPECTOR_SIZE_GROUPS (iw->size_groups), selected); gtk_inspector_data_list_set_object (GTK_INSPECTOR_DATA_LIST (iw->data_list), selected); gtk_inspector_actions_set_object (GTK_INSPECTOR_ACTIONS (iw->actions), selected); + gtk_inspector_menu_set_object (GTK_INSPECTOR_MENU (iw->menu), selected); gtk_inspector_gestures_set_object (GTK_INSPECTOR_GESTURES (iw->gestures), selected); notebook = gtk_widget_get_parent (iw->prop_list); @@ -198,6 +200,7 @@ gtk_inspector_window_class_init (GtkInspectorWindowClass *klass) gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, size_groups); gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, data_list); gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, actions); + gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, menu); gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, gestures); gtk_widget_class_bind_template_callback (widget_class, on_inspect); diff --git a/gtk/inspector/window.h b/gtk/inspector/window.h index f7d531dbf5..6b0e9d012a 100644 --- a/gtk/inspector/window.h +++ b/gtk/inspector/window.h @@ -54,6 +54,7 @@ typedef struct GtkWidget *size_groups; GtkWidget *data_list; GtkWidget *actions; + GtkWidget *menu; GtkWidget *gestures; GtkWidget *widget_popup; diff --git a/gtk/inspector/window.ui b/gtk/inspector/window.ui index 81b6516026..a38dc9ea72 100644 --- a/gtk/inspector/window.ui +++ b/gtk/inspector/window.ui @@ -239,6 +239,20 @@ Actions + + + + + True + True + + + + + True + Menu + + widget_tree diff --git a/po/POTFILES.in b/po/POTFILES.in index 5427a2351b..4cc08c262d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -281,6 +281,7 @@ gtk/inspector/data-list.ui.h gtk/inspector/general.ui.h gtk/inspector/gestures.c gtk/inspector/inspect-button.c +gtk/inspector/menu.ui.h gtk/inspector/object-hierarchy.ui.h gtk/inspector/prop-editor.c gtk/inspector/prop-list.ui.h -- 2.30.2